package com.person.android.utils;

import com.person.android.domain.AudioInfo;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @autor zhaokang.
 * @time on 2015/12/20.
 */
public class FileHeaderParserUtils {

    public static AudioInfo parseWavFile(String fileWavPath) throws IOException {
        AudioInfo result = new AudioInfo();
        File file = new File(fileWavPath);
        RandomAccessFile randomFile = new RandomAccessFile(file, "r");

        byte[] channelByte = new byte[2];
        randomFile.seek(0x16);
        randomFile.read(channelByte);
        result.numChannels = ByteUtils.bytes2short(channelByte);

        byte[] sampleRate = new byte[4];
        randomFile.seek(0x18);
        randomFile.read(sampleRate);
        result.sampleRate = ByteUtils.bytes2int(sampleRate);

        byte[] byteRate = new byte[4];
        randomFile.seek(0x1c);
        randomFile.read(byteRate);
        int tempByteRate = ByteUtils.bytes2int(byteRate);
        result.bitRate = tempByteRate / 1000;

        byte[] bitsPerSample = new byte[2];
        randomFile.seek(0x22);
        randomFile.read(bitsPerSample);
        result.bitsPerSample = ByteUtils.bytes2short(channelByte);

        result.mode = 1;
        result.quality = 5;

        return result;
    }

}
